package com.deng.common.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.deng.common.SQLHelper;
import com.deng.common.dao.IOrganization;
import com.deng.common.data.ComparatorFullOrganizationName;
import com.deng.common.vo.DispenseOrganizationVo;
import com.deng.common.vo.HisOrganizationVo;
import com.deng.common.vo.OrganizationVo;
import com.deng.util.FunctionFilter;

public class OrganizationImpl 
	extends SQLHelper 
	implements IOrganization {

	private String getSqlGuikouId(String guikou_id)
	{
		String replaceGuikouId = "";
		if(guikou_id == null || guikou_id.trim().equals(""))
		{
			return null;
		}
		else if(guikou_id.equals("0"))
		{
			return "0";
		}
		else
		{
			replaceGuikouId = guikou_id.replaceAll(":", ",");
			if(",".equals(replaceGuikouId.charAt(replaceGuikouId.length()-1)+""))
			{
				replaceGuikouId = replaceGuikouId.substring(0,replaceGuikouId.length()-1);
			}			
		}
		return replaceGuikouId;
	}
	public List<OrganizationVo> getMyGuikou(String guikouId)
	{
		try{
			Connection conn = getConnect();
			ResultSet rs = queryData(conn," select * from   t_sys_stat_department    " +
					"	where id in(" +getSqlGuikouId(guikouId)+")"+
					" 	order by name ");	
			List<OrganizationVo> organizationTable = new ArrayList<OrganizationVo>();
			while(rs.next()){
				OrganizationVo vo = new OrganizationVo();
				vo.id = rs.getInt("id");
				vo.name = rs.getString("name");	
				vo.duty_id = rs.getInt("duty_id");
				vo.newCode = rs.getString("newCode");
				organizationTable.add(vo);
			}
			freeConnection(conn);
			return organizationTable;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
		
	}
	
	public List<OrganizationVo> getAllOrganization() {
		try{
			Connection conn = getConnect();
			ResultSet rs = queryData(conn," select t_sys_stat_department.id,t_sys_stat_department.name," +
					" t_sys_stat_department.parent_id, t_sys_stat_department.is_leaf," +
					" case when base_bonus_function_detail is null then 0 else 1 end as isDef, " +
					" t_sys_stat_department.base_bonus_function," +
					" t_sys_stat_department.base_bonus_function_detail," +
					" t_sys_stat_department.duty_id ,newCode" +
					" from   t_sys_stat_department    " +
					" order by t_sys_stat_department.newCode,t_sys_stat_department.name ");	
			List<OrganizationVo> organizationTable = new ArrayList<OrganizationVo>();
			while(rs.next()){
				OrganizationVo vo = new OrganizationVo();
				vo.id = rs.getInt("id");
				vo.name = rs.getString("name");
				vo.parent_id = rs.getInt("parent_id");
				vo.is_leaf = rs.getInt("is_leaf");
				vo.isDef = rs.getInt("isDef");
				vo.duty_id = rs.getInt("duty_id");
				vo.newCode = rs.getString("newCode");
				vo.base_bonus_function_detail = rs.getString("base_bonus_function_detail");
				organizationTable.add(vo);
			}
			freeConnection(conn);
			return organizationTable;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	

	public OrganizationVo getOrganizationById(int id) {
		// TODO Auto-generated method stub
		try{
			Connection conn = getConnect();
			ResultSet rs = queryData(conn,"select t_sys_stat_department.id,			" +
					" t_sys_stat_department.name,			" +
					" t_sys_stat_department.parent_id,			" +
					" t_sys_stat_department.is_leaf,			" +
					" t_sys_stat_department.base_bonus_function," +
					" t_sys_stat_department.base_bonus_function_detail,			" +
					" t_sys_stat_department.duty_id, " +
					" case when base_bonus_function_detail is null then 0 else 1 end as isDef " +
					" from t_sys_stat_department			" +
					" where t_sys_stat_department.id = "+id+
					" order by isDef");		
			if(rs.next()){
				OrganizationVo vo = new OrganizationVo();
				vo.id = rs.getInt("id");
				vo.name = rs.getString("name");
				vo.parent_id = rs.getInt("parent_id");
				vo.is_leaf = rs.getInt("is_leaf");
				vo.isDef = rs.getInt("isDef");
				vo.base_bonus_function_detail = rs.getString("base_bonus_function_detail");
				vo.base_bonus_function = rs.getString("base_bonus_function");
				vo.duty_id = rs.getInt("duty_id");
				freeConnection(conn);
				return vo;
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}

	public OrganizationVo updateOrganizationBaseBonus(OrganizationVo vo){//实际表示的是奖金分配基数的获取
		try{
			Connection conn = getConnect();
			int i = updateData(conn,"update t_sys_stat_department " +
					"	set base_bonus_function = '"+vo.base_bonus_function+"'" +
					"	where id = "+vo.id);
			freeConnection(conn);
			if(i == 1){				
				return getOrganizationById(vo.id);
			}else{
				return null;
			}			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public OrganizationVo updateOrganizationBaseBonusFunction(OrganizationVo vo) {
		try{
			Connection conn = getConnect();
			int i = updateData(conn,"update t_sys_stat_department " +
					"	set base_bonus_function_detail = '"+vo.base_bonus_function_detail+"'" +
					"	where id = "+vo.id);
			freeConnection(conn);
			if(i == 1){				
				return getOrganizationById(vo.id);
			}else{
				return null;
			}			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}

	public List<OrganizationVo> addOneAndGetAllOrganization(OrganizationVo vo) {//增加科室
		try{
			Connection con = getConnect();
			ResultSet rs = queryData(con,"select case when max(id)+1 is null then 1 else max(id)+1 end as id" +
					" from t_sys_stat_department");	
			int maxID = 0;
			if(rs.next()){
				maxID = rs.getInt("id");
			}
			
			String sql ="insert into t_sys_stat_department(name,parent_id,is_leaf,apply_flag,sjly,duty_id,id,newCode)" +
					"values(?,?,?,?,?,?,?,?)";
			try{
				con.setAutoCommit(false);
				PreparedStatement pst=con.prepareStatement(sql);
				pst.setString(1, vo.name);
				pst.setInt(2, vo.parent_id);
				pst.setInt(3, vo.is_leaf);
				pst.setInt(4, 1);
				pst.setInt(5, 0);
				pst.setInt(6, vo.duty_id);
				pst.setInt(7, maxID);
				if(vo.newCode != null && !vo.newCode.equals(""))
				{
					pst.setString(8, vo.newCode);
				}
				else
				{
					pst.setString(8, vo.id+"");
				}
				
				pst.executeUpdate();
				pst.close();
				con.commit();
				
				updateData(con,"insert into t_sys_stat_department_vs_original_department(stat_id,original_id)" +
						"select "+maxID+",original_id  from t_sys_stat_department_vs_original_department where stat_id = "+vo.parent_id);
				
				return getAllOrganization();
			}catch(Exception ex){
				ex.printStackTrace();
				try {
					con.rollback();
					freeConnection(con);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			finally{
				try {
//					con.close();
					freeConnection(con);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}

	public List<OrganizationVo> deleteOneAndGetAllOrganization(OrganizationVo vo) {
		try{
			Connection conn = getConnect();
			if(vo.is_leaf == 1){
				int i = updateData(conn,"delete t_sys_stat_department " +
						"	where id = "+vo.id);
				
				updateData(conn,"delete t_sys_stat_department_vs_original_department" +
						"  where stat_id = "+vo.id);
				
				if(i == 1){
					return getAllOrganization();
				}else{
					freeConnection(conn);
					return null;
				}
			}else{
				ResultSet rs = queryData(conn,"select top 1 * from t_sys_stat_department where parent_id = "+vo.id);
				if(rs.next()){
					freeConnection(conn);
					return null;
				}else{
					int i = updateData(conn,"delete t_sys_stat_department " +
							"	where id = "+vo.id);
					updateData(conn,"delete t_sys_stat_department_vs_original_department" +
							"  where stat_id = "+vo.id);
					
					if(i == 1){
						return getAllOrganization();
					}else{
						freeConnection(conn);
						return null;
					}
				}
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}

	
	
	public List<OrganizationVo> getAllOrganizationByFullName(int is_leaf) {//返回全部科室/叶子节点科室  两种全部名称
		try{
			String sql = "";
			if(is_leaf != 1){
				sql = "exec SP_GetFullNameOrganization 0";
			}else{
//				sql = "exec SP_GetFullNameOrganization 1";
//		SQL 2005 is fine		sql = "select id,name,duty_id,parent_id,tp+'-'+name as full_name,base_bonus_function_detail" +
				sql = "select id,name,duty_id," +
					" (select name from DICT_DUTY_TYPE where id = duty_id) as duty_name,parent_id,cast(tp as nvarchar)+'-'+cast(name as nvarchar) as full_name," +
					" base_bonus_function_detail," +
					" (select count(1) from dbo.UNIT_PRICE_RULE_GROUP where organization_id=id) as ruled,newCode" +
					" from" +
					" (" +
					" select id,name,duty_id,parent_id," +
					" (select name from DICT_DUTY_TYPE where DICT_DUTY_TYPE.id = t_sys_stat_department.duty_id)  as tp," +
					" base_bonus_function_detail,newCode from t_sys_stat_department" +
					" where is_leaf = 1" +
					" ) as a" +
					" order by full_name";
			}
			Connection conn = getConnect();
			ResultSet rs = queryData(conn,sql);
			List<OrganizationVo> fullNameOrganizationList = new ArrayList<OrganizationVo>();
			while(rs.next()){
				OrganizationVo vo = new OrganizationVo();
				vo.id = rs.getInt("id");
				vo.name = rs.getString("name");
				vo.parent_id = rs.getInt("parent_id");
				vo.duty_id = rs.getInt("duty_id");
				vo.duty_name = rs.getString("duty_name");
				vo.full_name = rs.getString("full_name");
				vo.newCode = rs.getString("newCode");
				vo.base_bonus_function_detail = rs.getString("base_bonus_function_detail")!=null?rs.getString("base_bonus_function_detail"):"";
				vo.ruled = rs.getInt("ruled");
				fullNameOrganizationList.add(vo);
			}
			freeConnection(conn);
			return fullNameOrganizationList;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}

	public List<OrganizationVo> getTopOrganization() {//获得全院下属第一层的科室
		try{
			String sql = "select * from t_sys_stat_department where parent_id = 1 order by name";
			Connection conn = getConnect();
			ResultSet rs = queryData(conn,sql);
			List<OrganizationVo> lst = new ArrayList();
			while(rs.next()){
				OrganizationVo vo = new OrganizationVo();
				vo.id = rs.getInt("id");
				vo.name = rs.getString("name");
				vo.parent_id = rs.getInt("parent_id");
				vo.duty_id = rs.getInt("duty_id");
				lst.add(vo);
			}
			freeConnection(conn);
			return lst;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}

	public List<HisOrganizationVo> getHisCode(int id) {//通过id获得his的编码
		try{
			String sql = "select "+id+" as id,'organization' as type," +
					"	case when t_sys_stat_department_vs_original_department.original_id is not null then 1 else 0 end as selected," +
					"	t_sys_his_department.his_dept_code,t_sys_his_department.his_dept_name" +
					"	from t_sys_his_department left join t_sys_stat_department_vs_original_department" +
					"	on t_sys_stat_department_vs_original_department.original_id" +
					"	= t_sys_his_department.his_dept_code" +
					"	and t_sys_stat_department_vs_original_department.stat_id = "+id;
			Connection conn = getConnect();
			ResultSet rs = queryData(conn,sql);
			List<HisOrganizationVo> s = new ArrayList<HisOrganizationVo>();
			HisOrganizationVo hovo ;
			while(rs.next()){
				hovo = new HisOrganizationVo();
				hovo.code = rs.getString("his_dept_code");
				hovo.name = rs.getString("his_dept_name");
				hovo.selected = rs.getInt("selected");
				hovo.type = rs.getString("type");
				hovo.id = rs.getInt("id");
				s.add(hovo);
			}
			freeConnection(conn);
			return s;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}

	public void addHisCode(HisOrganizationVo vo) {
		try{
			String delSql = "insert into t_sys_stat_department_vs_original_department" +
					"	(stat_id,original_id)" +
					"	values("+vo.id+",'"+vo.code+"')";
			Connection conn = getConnect();
			updateData(conn,delSql);			
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void deleteHisCode(HisOrganizationVo vo) {
		try{
			String delSql = "delete t_sys_stat_department_vs_original_department" +
			"	where stat_id = "+vo.id+" and original_id = '"+vo.code+"'";
			Connection conn = getConnect();
			updateData(conn,delSql);
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public List<DispenseOrganizationVo> getAllDispenseOrganization() {//获得全部“工作绩效预处理单元”
		try{
			
			Connection conn = getConnect();
			List<DispenseOrganizationVo> l = getAllDispenseOrganization(conn);
			freeConnection(conn);
			return l;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	public List<DispenseOrganizationVo> getAllDispenseOrganization(Connection conn)
	{
		try
		{
			String sql = "select t_sys_stat_dispense.id,t_sys_stat_dispense.name,t_sys_stat_dispense.duty_id," +
					" t_sys_stat_organization_vs_dispense_organization.stat_id" +
					" from dbo.t_sys_stat_dispense" +
					" left join t_sys_stat_organization_vs_dispense_organization" +
					" on t_sys_stat_dispense.id = t_sys_stat_organization_vs_dispense_organization.dispense_id" +
					" order by t_sys_stat_dispense.duty_id,t_sys_stat_dispense.id ";
			ResultSet rs = queryData(conn,sql);
			List<DispenseOrganizationVo> l = new ArrayList<DispenseOrganizationVo>();
			DispenseOrganizationVo tmpVo = null;
			while(rs.next()){
				int id = rs.getInt("id");
				String name = rs.getString("name");
				int duty_id = rs.getInt("duty_id");
				int stat_id = rs.getInt("stat_id");
				if(tmpVo != null && id == tmpVo.id)//同一个节点
				{
					tmpVo.new_related_id += ":"+stat_id+":";
				}
				else//两种情况：
				{
					if(tmpVo != null)//第一次  tmpVo＝null    ；//与前一节点不同 tmpVo！＝null and id不相同
					{
						l.add(tmpVo);					
					}
					tmpVo = new DispenseOrganizationVo();
					tmpVo.id = id;
					tmpVo.name = name;
					tmpVo.duty_id = duty_id;
					if(stat_id != 0)
					{
						tmpVo.new_related_id = ":"+stat_id+":";
					}
				}
			}
			if(tmpVo != null)
			{
				l.add(tmpVo);
			}			
			return l;
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		return null;
		
	}
	public List<DispenseOrganizationVo> addAndGetDispenseOrganization(
			DispenseOrganizationVo vo) {
		try{
			Connection con = getConnect();
//			ResultSet rs = queryData(con,"select case when max(id)+1 is null then 1 else max(id)+1 end as id" +
//					" from t_sys_stat_organization_vs_dispense_organization");	
//			int maxID = 0;
//			if(rs.next()){
//				maxID = rs.getInt("id");
//			}
//			String sql = "insert into t_sys_stat_organization_vs_dispense_organization" +
//					" (id,name,duty_id) " +
//					" values("+maxID+",'"+vo.name+"',"+vo.duty_id+")";
			ResultSet rs = queryData(con,"select case when max(id)+1 is null then 1 else max(id)+1 end as id" +
					" from t_sys_stat_dispense");	
			int maxID = 0;
			if(rs.next()){
				maxID = rs.getInt("id");
			}
			String sql = "insert into t_sys_stat_dispense" +
					" (id,name,duty_id) " +
					" values("+maxID+",'"+vo.name+"',"+vo.duty_id+")";
			updateData(con,sql);
			
			
			List<DispenseOrganizationVo> l = getAllDispenseOrganization(con);
			
			freeConnection(con);
			return l;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}

	

	public List<DispenseOrganizationVo> updateAndGetDispenseOrganization(
			DispenseOrganizationVo vo) {
		try{
			Connection con = getConnect();
			String sql = "update t_sys_stat_dispense set name = '"+vo.name+"' " +
					"  where id = "+vo.id;
			updateData(con,sql);
			
			
			List<DispenseOrganizationVo> l = getAllDispenseOrganization(con);
			
			freeConnection(con);
			return l;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}

	public void updateDispenseBaseBonusFunction(DispenseOrganizationVo vo) {
		try{
			Connection conn = getConnect();
			int i = updateData(conn,"update t_sys_stat_organization_vs_dispense_organization " +
					"	set formula = '"+vo.formula_new.replaceAll("'", "''")+"'" +
					"	where id = "+vo.id);
			freeConnection(conn);
//			if(i == 1){				
//				return getOrganizationById(vo.id);
//			}else{
//				return null;
//			}			
		}catch(Exception ex){
			ex.printStackTrace();
		}
//		return null;
	}

	public List<DispenseOrganizationVo> getAllDispenseBonus(String stat_time_str) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<OrganizationVo> updateOneAndGetAllOrganization(OrganizationVo vo) {
		try{
			Connection con = getConnect();
			
			String sql ="update t_sys_stat_department set name = '"+vo.name+"'," +
					"	is_leaf = "+vo.is_leaf+",apply_flag = 1,sjly = 1," +
					"	duty_id = "+vo.duty_id;
			if(vo.newCode != null && !vo.newCode.equals(""))
			{
				sql += " , newCode = '"+vo.newCode+"'";
			}else
			{
				sql += " , newCode = '"+vo.id+"'";
			}
			sql+=" where id = " +vo.id;
			updateData(con,sql);
			return getAllOrganization();
		}catch(Exception ex){
				ex.printStackTrace();
		}
		return null;
	}



	public void updateDispenseRelatedId(OrganizationVo vo,int add) {
		try{
			Connection conn = getConnect();
			String oId = ":"+vo.id+":";
			int dispenseId = vo.dispense_id;		
			String sql = "select related_organization_id " +
					"	from t_sys_stat_organization_vs_dispense_organization" +
					"	where dispense_id = "+dispenseId;
			String sql2 = "";
			ResultSet rs = queryData(conn, sql);
			if(rs.next()){
				if(add == 1)//全新增
				{
					sql2 = "insert into t_sys_stat_organization_vs_dispense_organization" +
					"	(stat_id,dispense_id) values ("+vo.id+","+vo.dispense_id+")";
				}
				else//删除
				{
					sql2 = "delete t_sys_stat_organization_vs_dispense_organization" +
					"	where stat_id = "+vo.id+" and dispense_id ="+vo.dispense_id;
				}
//				String relatedOrgId = rs.getString("related_organization_id");
//				if((relatedOrgId == null || relatedOrgId.equals("")) && add == 1){//全新增
//					sql2 = "update t_sys_stat_organization_vs_dispense_organization" +
//							"	set related_organization_id = '"+oId+"'" +
//							"	where id = "+dispenseId;
//				}else if(relatedOrgId != null && !relatedOrgId.equals("") && add == 1){//原有，在新增
//					sql2 = "update t_sys_stat_organization_vs_dispense_organization" +
//					"	set related_organization_id = '"+relatedOrgId+oId+"'" +
//					"	where id = "+dispenseId;
//				}else if(relatedOrgId != null && !relatedOrgId.equals("") && add == 0){//删除一个
//					sql2 = "update t_sys_stat_organization_vs_dispense_organization" +
//					"	set related_organization_id = replace(related_organization_id,'"+oId+"','')"+
//					"	where id = "+dispenseId;
//				}
				System.out.println(sql2);
				updateData(conn, sql2);
				freeConnection(conn);
			}
		}catch(Exception ex){
				ex.printStackTrace();
		}
	}



	public void synchronizeDispenseAndOrganization() 
	{
		try
		{
			Connection conn = getConnect();				
			String sql = "truncate table t_sys_stat_dispense";
			updateData(conn, sql);
			sql = "truncate table t_sys_stat_organization_vs_dispense_organization";
			updateData(conn, sql);
			sql = "insert into t_sys_stat_dispense(id,name,duty_id) " +
					"	select id,cast((select name from DICT_DUTY_TYPE" +
					"	where id = duty_id) as nvarchar)+'-'+name,duty_id" +
					"   from t_sys_stat_department where is_leaf = 1";
			updateData(conn, sql);
			sql = "insert into t_sys_stat_organization_vs_dispense_organization" +
					"	(dispense_id,stat_id,duty_id)" +
					"	select id,id,duty_id from t_sys_stat_dispense";
			updateData(conn, sql);
			freeConnection(conn);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	public List<OrganizationVo> getLeafOrganization(){
		try
		{
			Connection conn = getConnect();				
			String sql = "select id,name," +
					"(select name from dict_duty_type where id = duty_id) as duty_name" +
					" from t_sys_stat_department" +
					" where is_leaf = 1" +
					" order by duty_name,name";

			ResultSet rs = queryData(conn,sql);	
			List<OrganizationVo> organizationTable = new ArrayList<OrganizationVo>();
			while(rs.next()){
				OrganizationVo vo = new OrganizationVo();
				vo.id = rs.getInt("id");
				vo.name = rs.getString("name");
				vo.duty_name = rs.getString("duty_name");
				vo.full_name = vo.duty_name+"-"+vo.name;
				organizationTable.add(vo);
			}
			freeConnection(conn);
			return organizationTable;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public List<OrganizationVo> getMyKaoheLeafOrganization(String user_name){
		try
		{
			Connection conn = getConnect();				
			String sql = "select uvd.kaohe_id as id,d.name,td.name as duty_name,td.name+'-'+d.name as full_name" +
					"	from" +
					"	(" +
					"	select distinct kaohe_id from dbo.KPI_VS_ORGANIZATION where guikou_id in" +
					"	(select d.id from t_sys_users_vs_department as uvd" +
					"	left join t_sys_stat_department as d" +
					"	on uvd.organization_id = d.id" +
					"	where code = '"+user_name+"')" +
					"	union" +
					"	select distinct kaohe_id from dbo.KPI_DETAIL_VS_ORGANIZATION where guikou_id in" +
					"	(select d.id from t_sys_users_vs_department as uvd" +
					"	left join t_sys_stat_department as d" +
					"	on uvd.organization_id = d.id" +
					"	where code = '"+user_name+"')" +
					"	)" +
					"	as uvd" +
					"	left join t_sys_stat_department as d" +
					"	on uvd.kaohe_id = d.id" +
					"	left join DICT_DUTY_TYPE as td" +
					"	on td.id = d.duty_id" +
					"	order by full_name";

			ResultSet rs = queryData(conn,sql);	
			List<OrganizationVo> organizationTable = new ArrayList<OrganizationVo>();
			while(rs.next()){
				OrganizationVo vo = new OrganizationVo();
				vo.id = rs.getInt("id");
				vo.name = rs.getString("name");
				vo.duty_name = rs.getString("duty_name");
				vo.full_name = rs.getString("full_name");
				organizationTable.add(vo);
			}
			freeConnection(conn);
			return organizationTable;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
}
